import path from 'path'
import Router from 'koa-router'
import Koa from 'koa'
import glob from 'glob'
import { initDecoratorRouter } from './routerDecorator'

class AllRouterLoader {
  static allRouterLoader = new AllRouterLoader()
  app!: Koa
  init(app: Koa) {
    this.app = app
    const rootRouter = new Router()
    initDecoratorRouter(rootRouter)
    this.importAllController()
    this.app.use(rootRouter.routes())
    this.app.use(rootRouter.allowedMethods())
  }
  importAllController() {
    // 获取所有的 controller 文件 然后引入执行里面的代码
    const files = glob.sync('src/modules/*/controller/*.controller.ts')
    files.map((file) => {
      // 获取绝对路径
      const absolutePath = path.join(process.cwd(), file)
      require(absolutePath)
    })
  }
}
export default AllRouterLoader.allRouterLoader
